import { getAreaObj, regularRule } from '@/utils/util'
import { getStore } from '@/utils/store'
import { operateFormConfig } from '@/views/elderlyInstitute/admissionManagement/config'
import { cloneDeep } from 'lodash-es'

const userInfo = getStore('userInfo') || {}

const tableConfig = {
	stripe: true,
	border: true,
	maxHeight: '80vh',
	columns: [
		{
			label: '业务类型',
			prop: 'businessTypeLabel',
		},
		{
			label: '所属区域',
			prop: 'ruleAreaName',
		},
		{
			label: '添加时间',
			prop: 'createTime',
		},
		{
			label: '操作',
			prop: 'menu',
			width: 300,
			fixed: 'right',
		},
	],
}

export const formConfig = {
	inline: false,
	showBtn: true,
	showReset: true,
	backBtnText: '重置',
	labelPosition: 'left',
	labelWidth: '120px',
	column: [
		{
			label: '业务类型',
			prop: 'businessType',
			propName: 'businessTypeLabel',
			type: 'remoteSelect',
			code: 'BUSINESSTYPE',
			api: 'getDictItemList',
			span: 24,
			rules: [
				{
					required: true,
					message: '请选择业务类型',
					trigger: 'change',
				},
			],
		},
		{
			label: '所属区域',
			prop: 'rule_Area_Id',
			propName: 'rule_Area_Name',
			type: 'AplidAreaSelect',
			checkStrictly: false,
			regionGrade: 3,
			paramsOnj: {
				administrationPid: userInfo.authAreas || userInfo.authCity || undefined,
			},
			span: 24,
			rules: [
				{
					required: true,
					message: '请选择所属区域',
					trigger: 'change',
				},
			],
		},
		{
			label: '办理时间段',
			prop: 'createTimeArr',
			type: 'daterange',
			span: 24,
			valueFormat: 'YYYY-MM-DD',
			rules: [
				{
					required: true,
					message: '请选择办理时间段',
					trigger: 'change',
				},
			],
		},
		{
			label: '人额数量',
			prop: 'ruleLimit',
			type: 'input',
			rules: [
				{
					required: true,
					message: '请填写人额数量',
					trigger: 'blur',
				},
				{ pattern: regularRule.fixedTwoNum, message: '请输入正确的数值' },
			],
			span: 24,
		},
		{
			label: '办理年龄段',
			prop: 'age',
			tip: '请输入60-200之间的年龄值',
			type: 'RangeValue',
			leftMin: 60,
			leftMax: 200,
			rightMin: 60,
			rightMax: 200,
			span: 24,
		},
		{
			label: '凭证',
			prop: 'apiParams',
			// propName: '',
			type: 'remoteSelect',
			api: 'view_sysSsoConfig_page',
			filterKey: 'id',
			filterLabel: 'appName',
			multiple: false,
			rules: [{ required: true, message: '请输入凭证' }],
			span: 24,
			change: (item, form, value) => {},
		},
	],
}
const searchConfig = {
	inline: true,
	// 	showBtn: true,
	// 	labelPosition: 'left',
	column: [
		{
			label: '业务类型',
			prop: 'businessType',
			propName: 'businessTypeLabel',
			type: 'remoteSelect',
			code: 'BUSINESSTYPE',
			api: 'getDictItemList',
			span: 12,
		},
		{
			label: '所属区域',
			prop: 'rule_Area_Id',
			propName: 'rule_Area_Name',
			type: 'AplidAreaSelect',
			checkStrictly: true,
			regionGrade: 3,
			span: 12,
		},
		{
			label: '状态',
			prop: 'ruleEnable',
			type: 'select',
			dicData: [
				{ label: '启用', value: '1' },
				{ label: '禁用', value: '0' },
			],
			span: 12,
		},
		{
			label: '添加时间',
			prop: 'createTimeBetween',
			type: 'datetimerange',
			format: 'YYYY年MM月DD HH:mm:ss',
			valueFormat: 'YYYY-MM-DD HH:mm:ss',
			span: 12,
		},
	],
}
export const baseConfig: any = {
	pageName: '业务办理规则设置',
	addLabel: '新增',
	// dictcodes: [],
	// rowKey: 'id',
	// tipLabel: 'projectName',
	api: {
		index: 'view_bscBusinessRule_page',
		add: 'view_bscBusinessRule_add',
		detail:"view_bscBusinessRule_view",
		// export: 'view_ofsSubsidyApply_approval_exportApply'
		edit: 'view_bscBusinessRule_edit',
		// 	delete: 'view_ofsSubsidyProject_del',
	},
	auth: {
		index: 'view_bscBusinessRule_page',
		add: 'view_bscBusinessRule_add',
		detail:"view_bscBusinessRule_view",
		// mulitSet: 'view_ofsSubsidyApply_approval_exportApply'
		edit: 'view_bscBusinessRule_edit',
		// 	delete: 'view_ofsSubsidyProject_del',
	},

	menuBtn: [
		{
			icon: '',
			label: '编辑',
			type: 'primary',
			operate: 'edit',
			auth: 'view_ofsSubsidyApply_view',
			text: true,
			show: true,
		},
	],
	// dialogConfig: {
	// 	width: '65%',
	// },
	saveFormate: async (query) => {
		let tempAgeArr = null
		if (query.age) {
			tempAgeArr = query.age.split(',')
			query.ruleAgeStart = Number(tempAgeArr[0])
			query.ruleAgeEnd = Number(tempAgeArr[1])
		}
		if (query.ruleAgeStart < 60 || query.ruleAgeEnd > 200) {
			// ElMessage.error('办理年龄段请按要求填写')
		}

		if (query.createTimeArr) {
			query.ruleStartDate = query.createTimeArr[0]
			query.ruleEndDate = query.createTimeArr[1]
		}
		let ruleAreaIdList
		let ruleAreaNameList
		if (query.rule_Area_Id) {
			ruleAreaIdList = getAreaObj(query.rule_Area_Id, 'rule')
			ruleAreaNameList = getAreaObj(query.rule_Area_Name, 'rule')
		}
		return new Promise((resolve, reject) => {
			delete query.areas
			resolve({
				...query,
				...ruleAreaIdList,
				...ruleAreaNameList,
				rule_Area_Id: undefined,
				age: undefined,
				createTimeArr: undefined,
				rule_Area_Name: undefined,
			})
		})
	},
	beforeOnLoadFormate: (query) => {
		if (query.createTimeBetween) {
			query.createTimeBetween = query.createTimeBetween[0] + ',' + query.createTimeBetween[1]
		} else {
			query.createTimeBetween = undefined
		}
		let ruleAreaIdList
		let ruleAreaNameList
		if (query.rule_Area_Id) {
			ruleAreaIdList = getAreaObj(query.rule_Area_Id, 'rule')
			ruleAreaNameList = getAreaObj(query.rule_Area_Name, 'rule')
		}
		return new Promise((resolve, reject) => {
			resolve({
				...query,
				...ruleAreaIdList,
				...ruleAreaNameList,
				rule_Area_Id: undefined,
				rule_Area_Name: undefined,
			})
		})
	},
	operateFormate: async (form,type) => {
		let item = cloneDeep(form)
		item.age = `${item.ruleAgeStart},${item.ruleAgeEnd}`
		item.createTimeArr = [item.ruleStartDate, item.ruleEndDate]
		const area: any = getAreaObj(item, 'rule', 'set')
		return Promise.resolve({
			...item,
			rule_Area_Id: area.areaIds,
			rule_Area_Name: area.names,
		})
	},
	onLoadFormate: async (arr) => {
		const tableData = arr.map((item) => {
			item.age = `${item.ruleAgeStart},${item.ruleAgeEnd}`
			item.createTimeArr = [item.ruleStartDate, item.ruleEndDate]
			const area: any = getAreaObj(item, 'rule', 'set')
			return {
				...item,
				rule_Area_Id: area.areaIds,
				rule_Area_Name: area.names,
			}
		})
		return Promise.resolve(Object.freeze(tableData))
	},
	searchConfig,
	tableConfig,
	formConfig,
	// detailConfig,
}
